<template>
  <div>
    <Spin :spinning="loading" tip="Loading...">
      <div style="padding-top: 1rem;"></div>
      <!-- 条件筛选 -->
      <FormPanel ref="formPanel" :list="formList" :cols="2" :formType="'add'" labelWidth="110" :isSubmitBtn="false"
        @formChange="formChangeHandle" />
      <div style="padding-top: 1rem;"></div>
      <!-- table -->
      <BreakSpace label="备件采购渠道" :containerStyle="{ marginBottom: '10px' }" />
      <FilterTable ref="table" :columns="columns" :dataSource="dataList" :datakey="'records'" :isSelectColumn="false"
        :onColumnsChange="columns => (this.columns = columns)" :isToperInfo="false" :isColumnFilter="false"
        :height="172" style="margin-bottom:20px;" />
      <div :style="{
        position: 'absolute',
        left: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }">
       <!--  <multiuse-button type="primary" :click="saveDialog.bind()">保 存</multiuse-button> -->
        <multiuse-button @click="closeDialog">返 回</multiuse-button>
      </div>
      <BaseDialog :visible.sync="suppliers" :title="'供应商选择'" width="850px" destroyOnClose
        :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
        <SupplierList @close="suppliersClose" @save="suppliersSave" :rowDate="rowDate" selectionType="multiple">
        </SupplierList>
      </BaseDialog>
      <BaseDialog :visible.sync="visible" :title="'备件选择'" destroyOnClose width="850px"
        :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
        <Vehicle @close="close" @save="partSave"></Vehicle>
      </BaseDialog>
    </Spin>
  </div>
</template>
<script>
import { selectById} from '@/api/claim/factory/PartWarrantyType';
import {getSupplierList } from '@/api/dictionary/index';
import Vehicle from '@/pages/claimDemage/tools/vehicle.vue';
import { getAppId } from '@/assets/js/auth';
import { dictionary } from '@/utils/dictMixin';
import { getModel } from '@/api/customerRepair/customerCare/insuranceDueRemind/index';
import SupplierList from '@/pages/claimDemage/tools/SuppliersList.vue';
export default {
  mixins: [dictionary],
  components: {
    Vehicle,
    SupplierList
  },
  props: ['type', 'rowData'],
  data() {
    return {
      loading: false,
      dataList1: [],
      visible: false,
      supData: '',
      visibleDrawer: false,
      collapse: false,
      rowDate: {},
      suppliers: false,
      infoId: null,
      flterValue: {}, //参数
      fetchParams: {}, //ipa 接口
      selectedRows: [], //选中经销商
      formList: this.createFormList1(),
      saveData: null,
      dataList: [],
      columns: this.createTableColumns(),
      activeName: '审核记录',
      newVar: {}

    };
  },
  created() {
    // this.chuqi()
    this.getSeriesStr();
  },
  mounted() {
    this.init();
  },
  methods: {
    formChangeHandle(val) {
      console.log(val);
      this.saveData = val;
    },
    //搜索获取数据
    changeHandle(val) {
      this.fetchParams = val;
      console.log(val);
    },
    async init() {
      this.loading = true;
      let newVar = await selectById({ id: this.rowData.id });
      this.formList.forEach(x => {
        x.disabled =true;
        if (x.fieldName == 'series' && this.rowData.series != '' && this.rowData.series != null) {
          x.initialValue = newVar.data[x.fieldName].split(',');
        } else if (x.fieldName == 'manageRate') {
          //x.initialValue = (this.rowData.manageRate)*100;
          x.initialValue =parseFloat(this.rowData.manageRate * 100).toFixed(2)
        } else {
          x.initialValue = newVar.data[x.fieldName];
        }
      });
      //console.log('this.formList :'+ JSON.stringify(this.formList));
      //this.formList.find(x => x.fieldName == 'seriers').initialValue= this.rowData.manageRate;
      console.log('newVar.data', newVar.data.channelNameList)
      this.dataList = newVar.data.channelNameList
      //  console.log('this.rowData.manageRate',newVar.data.channelNameList)
      //  this.formList.find(x => x.fieldName == 'manageRate').initialValue= this.rowData.manageRate;
      this.supplierCode = newVar.data.supplierCode;
      this.newVar = newVar.data;
      this.loading = false;
    },
    // async chuqi() {
    //   const res = await getPartType({ type: 5586 });
    //   if (res.resultCode == 200) {
    //     this.formList.find(x => x.fieldName == 'type').initialValue = '';
    //     this.formList.find(x => x.fieldName == 'type').itemList = res.data.map(y => ({
    //       text: y.codeCnDesc,
    //       value: y.codeId
    //     }));
    //   }
    // },
    selectSuppliersCode() {
      this.suppliers = true;
    },
    suppliersClose() {
      this.suppliers = false;
    },
    suppliersSave(obj) {
      console.log(obj, 'obj')
      this.supData = ''
    //    for (var i = 0; i < obj.length; i++) {
    //    console.log('obj[i].supCode',obj[i].supCode)
    //     this.supData += obj[i].supCode + ','
    //  };
    this.supData = obj.map(item => item.supCode).join(',')
       //console.log('supData', this.supData)
     // console.log('obj[i].supCode', obj[i].supCode)
      this.formList.find(x => x.fieldName == 'supplierCode').initialValue = this.supData;
      //this.formList.find(x => x.fieldName == 'supplierName').initialValue = obj[0].supName;
      //this.supplierCode = obj[0].supCode;
     // this.supId = obj[0].supId;
      this.suppliers = false;
    },
    //保存
 /*    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData !== null) {
        // console.log(this.saveData,'this.saveData');
        this.saveData.supId = this.supId;
        //this.saveData.supplierCode = this.supplierCode;
        // this.saveData.typeId = this.rowData.typeId;
        this.saveData.ptId = this.rowData.ptId;
        this.saveData.isDeleted = this.rowData.isDeleted;
        // this.saveData.createdBy = this.rowData.createdBy;
        // this.saveData.createdAt = this.rowData.createdAt;
        this.saveData.updatedAt = null;
        this.saveData.updatedBy = null;
        this.saveData.supplierCode = this.supData
        this.saveData.series = this.saveData.series.toString()
        console.log(this.saveData,'this.saveData');
        for (let key in this.saveData) {
          this.newVar[key] = this.saveData[key];
        }
        this.loading = true;
        let res = await editSave(this.newVar);
        console.log(res, 'res');
        if (res.resultCode === 200) {
          this.$emit('save');
          return this.$notify.success({ title: '提示信息', message: '保存成功' });
        } else {
          this.loading = false;
        }
      }
    }, */
    //取消操作
    closeDialog() {
      this.$emit('close');
    },
    add() { },
    chooseOrderNo() {
      this.visible = true;
    },
    async partSave(obj) {
      console.log(obj);
      this.formList.find(x => x.fieldName == 'partCode').initialValue = obj[0].partCode;
      this.formList.find(x => x.fieldName == 'partName').initialValue = obj[0].partName;
      this.rowDate.partCode = obj[0].partCode;

      this.formList.find(x => x.fieldName == 'supplierCode').initialValue = '';
      //this.formList.find(x => x.fieldName == 'supplierName').initialValue = '';

      this.rowDate.partId = obj[0].partId;
      this.rowDate.partCode = obj[0].partCode;

      const res = await getSupplierList({ appId: getAppId(), id: obj[0].partId });

      if (res.resultCode == 200) {
        res.data.records.map(x => {
          x.supCode = x.bjgybm;
          x.supId = x.supplierId;
          x.supName = x.bjgyjc;
        })
        if (res.data.records.length == 1) {
          this.suppliersSave(res.data.records)
        } else if (res.data.records.length == 0) {
          this.rowDate.partCode = ""
        }
      }
      this.visible = false;
    },
    //适用车型下拉
    async getSeriesStr() {

      const res1 = await getModel();
      if (res1.resultCode == 200) {

        this.formList.find(x => x.fieldName == 'series').itemList = res1.data.map(y => ({ text: y.MODEL_NAME, value: y.MODEL_CODE }));
      }
    },
    close() {
      this.visible = false;
    },
    createFormList1() {
      return [
        {
          type: 'INPUT',
          label: '备件代码',
          fieldName: 'partCode',
          readonly: true,// 只读
          disabled: true,// 置会
          placeholder: '请选择备件代码...',
          rules: [{ required: false, message: '请选择备件代码...', trigger: 'blur' }],
          unitRender: p => {
            return (
              <div>
                <multiuse-button icon="el-icon-search" onClick={() => this.chooseOrderNo()} />
              </div>
            );
          }
        },
        {
          type: 'INPUT',
          label: '备件名称',
          readonly: true,// 只读
          disabled: true,// 置会
          fieldName: 'partName',
          placeholder: '请输入备件名称...',
          rules: [{ required: false, message: '请输入备件名称...', trigger: 'blur' }],
          //offsetRightCols: 1
        },
        {
          type: 'INPUT',
          label: '供应商编码',
          fieldName: 'supplierCode',
          placeholder: '请选择供应商...',
          readonly: true,
          rules: [{ required: false, multiple: true, message: '请选择供应商', trigger: 'blur' }],
          unitRender: p => {
            return (
              <div>
                <multiuse-button icon="el-icon-search" onClick={() => this.selectSuppliersCode()} />
              </div>
            );
          }
        },
        /*  {
           type: 'INPUT',
           label: '供应商名称',
           readonly: true,// 只读
           disabled: true,// 置会
           fieldName: 'supplierName',
           placeholder: '请输入供应商名称...',
           //offsetRightCols: 1
         }, */
        {
          type: 'INPUT',
          label: '备件管理费率',
          fieldName: 'manageRate',
          initialValue: '',
          placeholder: '请输入备件管理费率...',
         // rules: [{ required: true, message: '请输入备件管理费率...', trigger: 'blur' }, { pattern: /^(\d+)(\.?)(\d{0,2})$/, message: '请输入数字值,小数点后两位...', trigger: 'blur' }],
          unitRender: () => {
            return <span>(%)</span>;
          },
        },
        {
          type: 'SELECT',
          label: '返件类型',
          fieldName: 'returnType',
          placeholder: '请选择',
          initialValue: '',
          //rules: [{ required: true, message: '请选择...', trigger: 'blur' }],
          filterable: true,
          itemList: this.createDictList('5585', [55851002])
        },
        {
          type: 'SELECT',
          label: '备件类型',
          fieldName: 'warrantyType',
          placeholder: '请选择',
          initialValue: '',
         // rules: [{ required: true, message: '请选择...', trigger: 'blur' }],
          filterable: true,
          itemList: this.createDictList('5509')
        },
        {
          type: 'SELECT',
          label: '是否辅料',
          placeholder: '请选择...',
          fieldName: 'isAccessories',
         // rules: [{ required: true, message: '请选择...', trigger: 'blur' }],
          itemList: this.createDictList('1004')
        },
        {
          type: 'SELECT',
          label: '是否特批',
          placeholder: '请选择...',
          fieldName: 'isSpecially',
         // rules: [{ required: true, message: '请选择...', trigger: 'blur' }],
          itemList: this.createDictList('1004')
        },
        {
          type: 'MULTIPLE_SELECT',
          label: '适用车型',
          placeholder: '请选择...',
          fieldName: 'series',
          //rules: [{ required: false, message: '请选择...', trigger: 'blur' }],
          itemList: [],
        },
        {
          type: 'SELECT',
          label: '是否附件',
          placeholder: '请选择...',
          fieldName: 'isAttached',
          //rules: [{ required: true, message: '请选择...', trigger: 'blur' }],
          itemList: this.createDictList('1004')
        },
        {
          type: 'SELECT',
          label: '是否报废',
          fieldName: 'isScrap',
          initialValue: '',
          //rules: [{ required: false, message: '请选择...', trigger: 'blur' }],
          itemList: [
            { text: '是', value: 10041001 },
            { text: '否', value: 10041002 }
          ],
        },
        {
          type: 'SELECT',
          label: '状态',
          placeholder: '请选择...',
          fieldName: 'ptStatus',
         // rules: [{ required: true, message: '请选择...', trigger: 'blur' }],
          itemList: [
            { text: '启用', value: 10041001 },
            { text: '停用', value: 10041002 }
          ],
        },
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        { title: '采购渠道代码', dataIndex: 'channelCode', sorter: true },
        { title: '采购渠道名称', dataIndex: 'channelName', sorter: true },
      ]
    }

  }
};
</script>

<style lang="less" scoped>

</style>
